#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int findLHS(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int left = 0;
        int right = 0;
        int m = 0;
        while (right < nums.size())
        {
            if (nums[right] - nums[left] <= 1)
                right++;
            else
            {
                if (nums[right - 1] > nums[left])
                    m = max(m, right - left);
                left++;
            }
        }
        if (nums[right - 1] > nums[left])
            m = max(m, right - left);
        return m;
    }
};